<template>
  <div>
    <div class="inputStr">
      <el-input v-model="pagination.queryContent" clearable style="width: 150px" placeholder="请输入试卷名"></el-input>
      <el-button type="primary" icon="el-icon-search" style="margin: 10px" v-model="pagination.queryContent"
                 @click="findPage">搜索
      </el-button>
    </div>
    <el-table
        v-loading="loading"
        :data="RandomPaperList"
        style="width: 100%">
      <el-table-column
          prop="paperName"
          label="试题名称"
          align="center">
      </el-table-column>
      <el-table-column
          prop="startCheck"
          align="center"
          label="考试时间">
      </el-table-column>
      <el-table-column
          prop="checkTime"
          align="center"
          label="时长(分钟)">
      </el-table-column>
      <el-table-column
          align="center"
          prop="grandScore"
          label="总分">
      </el-table-column>
      <el-table-column
          align="center"
          label="试卷类型">
        <template slot-scope="scope">
          <span>{{ scope.row.paperType == 1 ? '随机卷' : '手动卷' }}</span>
        </template>
      </el-table-column>
      <el-table-column
          align="center"
          label="创建人">
        <template slot-scope="scope">
          <span v-html="scope.row.user.realName"></span>
        </template>
      </el-table-column>

      <el-table-column
          fixed="right"
          align="center"
          label="操作">
        <template slot-scope="scope">

          <el-button @click="testPaper(scope.row.id,scope.row.startCheck,scope.row.checkTime)"
                     :disabled="scope.row.startCheck | dateFilter2(scope.row.testUserIds,userId,scope.row.checkTime)"
                     type="primary">
            <span>{{ scope.row.startCheck | dateFilter(scope.row.testUserIds,userId,scope.row.checkTime) }}</span>
          </el-button>
          <!--                    <el-button type="danger" plain>删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <div style="padding-top: 40px">
      <el-pagination
          class="pagiantion"
          :total="total"
          @current-change="handleSelectionChange"
          :current-page="pagination.currentPage"
          :page-size="pagination.pageSize"
          layout="total, prev, pager, next, jumper"
      >
      </el-pagination>
    </div>

  </div>
</template>

<script>
export default {
  name: "PaperList",
  data() {
    return {
      loading: false,
      num: 0,
      RandomPaperList: [],
      total: 0,
      userId: sessionStorage.getItem("userId"),
      pagination: {
        currentPage: 1,
        pageSize: 5,
        queryContent: "",
        categoryId: null
      },
    }
  },
  filters: {
    dateFilter: function (value, testUserIds, userId, checkTime) {
      for (let i = 0; i < testUserIds.length; i++) {
        if (testUserIds[i] == userId) {
          return '已参加';
        }
      }
      let date = new Date(value);
      let dates = new Date(date)
      let min = date.getMinutes();
      date = new Date(date.setMinutes(min + checkTime));
      let nowTime = new Date();  //现在时间
      //console.log(date)  //结束时间
      //console.log(dates)  //开始时间
      if (nowTime > dates && nowTime < date) {
        return "开始考试";
      } else {
        return "不予开放";
      }
    },
    dateFilter2: function (value, testUserIds, userId, checkTime) {
      for (let i = 0; i < testUserIds.length; i++) {
        if (testUserIds[i] == userId) {
          return true;
        }
      }
      let date = new Date(value);
      let dates = new Date(date)
      let min = date.getMinutes();
      date = new Date(date.setMinutes(min + checkTime));
      let nowTime = new Date();  //现在时间
      //console.log(date)  //结束时间
      //console.log(dates)  //开始时间
      if (nowTime > dates && nowTime < date) {
        return false;
      } else {
        return true;
      }
    }
  },
  mounted() {
    this.findPage();
  },
  methods: {
    testPaper(id,startCheck,checkTime) {
      this.$router.push("/question/test?id=" + id+"&startCheck="+startCheck+"&checkTime="+checkTime);
    },
    handleSelectionChange(currentPage) {
      this.pagination.currentPage = currentPage;
      this.findPage();
    },
    findPage() {
      this.loading = true;
      this.axios.post("/paper/allPaperList/findPage", this.pagination).then(resp => {
        if (resp.data.statusCode == 200) {
          this.total = resp.data.data.total;
          //this.userId=sessionStorage.getItem("userId");
          //alert(this.userId)
          this.RandomPaperList = resp.data.data.data;
        }
        this.loading = false;
      })
    }
  }
}
</script>

<style scoped>
.inputStr {
  width: 550px;
  margin-left: 500px;
}
</style>
